package com.gscitysfy.cus.drain.modules.command.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gscitysfy.cus.drain.modules.command.convert.InstructCarConvert;
import com.gscitysfy.cus.drain.modules.command.dto.ResourceDispatchDto;
import com.gscitysfy.cus.drain.modules.command.entity.InstructCar;
import com.gscitysfy.cus.drain.modules.command.mapper.InstructCarMapper;
import com.gscitysfy.cus.drain.modules.command.service.IInstructCarService;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 资源调度 服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2022-10-24 18:57:15
 */
@AllArgsConstructor
@Service
public class InstructCarServiceImpl extends ServiceImpl<InstructCarMapper, InstructCar> implements IInstructCarService {

    private final InstructCarConvert instructCarConvert;

    @Override
    public List<InstructCar> fetchByDangerSituation(Long dangerSituationId) {
        LambdaQueryWrapper<InstructCar> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(InstructCar::getDangerSituationId, dangerSituationId);
        return baseMapper.selectList(queryWrapper);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public Boolean deleteBatch(List<Long> dangerSituationIdList) {
        LambdaQueryWrapper<InstructCar> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(InstructCar::getDangerSituationId, dangerSituationIdList);
        List<InstructCar> instructCarList = baseMapper.selectList(queryWrapper);
        instructCarList.forEach(instructCar -> {
            instructCar.setCmUsable(true);
        });
        return this.updateBatchById(instructCarList);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public Boolean saveBatch(ResourceDispatchDto dto) {
        if (CollectionUtils.isEmpty(dto.getInstructCarDtoList())) {
            return true;
        }
        List<InstructCar> instructCarList = instructCarConvert.toEntityList(dto);
        return this.saveBatch(instructCarList);
    }
}
